﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SecurityApi.DataAccess
{
    public class UserApplicationRepository : BaseRepository
    {

        public IQueryable<SecurityApi.Model.Application> GetUserApplications(string userName, int enterpriseId=1)
        {
            var query = from u in _context.EnterpriseUsers
                        join ua in _context.UserApplications on u.UserId equals ua.UserId
                        join a in _context.Applications on ua.ApplicationId equals a.ApplicationId
                        join e in _context.Enterprises on u.EnterpriseId equals e.EnterpriseId
                        where u.UserName == userName && u.EnterpriseId == enterpriseId
                        select new SecurityApi.Model.Application
                        {
                            ApplicationId = a.ApplicationId,
                            Title = a.Title,
                            Name = a.Name,
                            Active = a.Active,
                            Area = a.Area,
                            DefaltController = a.DefaltController,
                            DefaltAction = a.DefaltAction,
                            DisplayOrder = a.DisplayOrder,
                            Enterprise = new Model.Enterprise { Id = e.EnterpriseId, Name = e.Name, Title = e.Title, Active = e.Active }
                        };

            query = query.OrderBy(p => p.DisplayOrder);

            return query;
        }

        public IQueryable<SecurityApi.Model.Application> GetUserApplications(int userId, int enterpriseId = 1)
        {
            var query = from u in _context.EnterpriseUsers
                        join ua in _context.UserApplications on u.UserId equals ua.UserId
                        join a in _context.Applications on ua.ApplicationId equals a.ApplicationId
                        join e in _context.Enterprises on u.EnterpriseId equals e.EnterpriseId
                        where u.UserId == userId && u.EnterpriseId == enterpriseId
                        select new SecurityApi.Model.Application
                        {
                            ApplicationId = a.ApplicationId,
                            Title = a.Title,
                            Name = a.Name,
                            Active = a.Active,
                            Area = a.Area,
                            DefaltController = a.DefaltController,
                            DefaltAction = a.DefaltAction,
                            DisplayOrder = a.DisplayOrder,
                            Enterprise = new Model.Enterprise { Id = e.EnterpriseId, Name = e.Name, Title = e.Title, Active = e.Active }
                        };

            query = query.OrderBy(p => p.DisplayOrder);

            return query;
        }

        public Application GetApplicationByName(string applicationName, int enterpriseId = 1)
        {
            Application application = _context.Applications.Where(p => p.Name == applicationName && p.EnterpriseId == enterpriseId).SingleOrDefault();
            return application;
        }

        public SecurityApi.Model.Application GetApplicationById(int id)
        {
            SecurityApi.Model.Application app = null;
            Application e = _context.Applications.Where(p => p.ApplicationId == id).SingleOrDefault();

            if (null != e)
            {
                app = new Model.Application { ApplicationId = e.ApplicationId, Name = e.Name, Title = e.Title, Active = e.Active, Area = e.Area, DefaltAction = e.DefaltAction, DefaltController = e.DefaltController };
            }
            return app;
        }

        public IQueryable<SecurityApi.Model.Application> GetAll()
        {
            var query = from e in _context.Applications select new SecurityApi.Model.Application { ApplicationId = e.ApplicationId,Name =e.Name, Title=e.Title,Active = e.Active, Area = e.Area,DefaltAction=e.DefaltAction,DefaltController=e.DefaltController };
            return query;
        }

    }
}
